//
//  ICMessageSearchController.m
//  ICome
//
//  Created by ENN on 16/5/11.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICMessageSearchController.h"
#import "ICTitleView.h"
#import "ICMessageTableViewCell.h"
#import "ICSearchMessage.h"
#import "ICMessageDatabase.h"
#import "ICMessageFrame.h"
#import "ICMessageModel.h"
#import "ICMessage.h"
#import "ICChatViewController.h"
#import "ICGroup.h"

@interface ICMessageSearchController ()<UITableViewDataSource,UITableViewDelegate,ICTitleViewDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, strong)ICEmptyStyleView *emptyView;

@property (nonatomic, strong) ICTitleView *titleView;
@end

@implementation ICMessageSearchController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupSubView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - Table view data source | delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ICMessageTableViewCell * cell = [ICMessageTableViewCell cellWithTableView:tableView];
    if (indexPath.row == self.dataArr.count - 1) {
        [cell setBottomLineStyle:CellLineStyleNone];
    }
    else {
        [cell setBottomLineStyle:CellLineStyleDefault];
    }
    cell.searchMessage = self.dataArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 得到滚动的位置
    [self.titleView.searchBar resignFirstResponder];
    
    ICSearchMessage *searchMessage = self.dataArr[indexPath.row];
    WEAKSELF
    [ICMessageDatabase selectAllMessage:self.group.gId finish:^(NSArray *array, NSError *error) {
        for (long i = 0; i < array.count; i ++) {
            ICMessageFrame *messageF = array[i];
            if ([messageF.model.message.messageId isEqualToString:searchMessage.messageId]) {
                ICChatViewController *chatVC = [[ICChatViewController alloc] init];
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                chatVC.group           = weakSelf.group;
                chatVC.searchArr       = [NSMutableArray arrayWithArray:array];
                chatVC.indexPath       = indexPath;
                [weakSelf.navigationController pushViewController:chatVC animated:YES];
                break;
            }
        }
    }];
}

- (UIView*)findFirstResponderBeneathView:(UIView*)view
{
    // Search recursively for first responder
    for ( UIView *childView in view.subviews ) {
        if ( [childView respondsToSelector:@selector(isFirstResponder)] && [childView isFirstResponder] )
            return childView;
        UIView *result = [self findFirstResponderBeneathView:childView];
        if ( result )
            return result;
    }
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.titleView.searchBar resignFirstResponder];
}

#pragma mark - Private

- (void)setupSubView
{
    self.view.backgroundColor = BACKGROUNDCOLOR;
    self.navigationController.navigationBarHidden = YES;
    self.titleView = [[ICTitleView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, [ICTools getNavbarHeight]+[ICTools getStatusbarHeight])];
    self.titleView.delegate     = self;
    self.titleView.backgroundColor  = NE_BACKGROUND_COLOR;
    self.titleView.searchBar.backgroundColor = [ICTools getRgbColor:0x5CACFE];
    self.titleView.searchBar.tintColor = [UIColor whiteColor];
    self.titleView.searchBar.textColor = [UIColor whiteColor];
    [self.titleView.searchBar setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    UIImage *image = [UIImage  imageNamedFromiComeSDKBundle:@"icon_search"];
    self.titleView.searchBar.iconImage.image = [image imageWithColor:[UIColor whiteColor]];
    [self.view addSubview:self.titleView];
    
    UITableView *tableView = [[UITableView alloc] init];
    tableView.frame        = CGRectMake(0, self.titleView.bottom, self.view.width, self.view.height-self.titleView.height);
    tableView.delegate     = self;
    tableView.dataSource   = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tableView];
    self.tableView         = tableView;
}

#pragma mark - titleViewDelegate

- (void)cancelBtnClicked
{
    [self.titleView.searchBar resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchText:(NSString *)text
{
    self.dataArr  = nil;
    WEAKSELF
    [MBProgressHUD showMessage:@"" toView:self.tableView];
    [ICMessageDatabase searchMessage:self.group.gId searchTxt:text finish:^(NSArray *array, NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.tableView];
        if (array.count == 0) {
            weakSelf.emptyView.hidden = NO;
        } else {
            weakSelf.emptyView.hidden = YES;
        }
        weakSelf.dataArr = array;
        [weakSelf.tableView reloadData];
    }];
}

#pragma mark - Getter

- (NSArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSArray array];
    }
    return _dataArr;
}

-(ICEmptyStyleView *)emptyView {
    if (!_emptyView) {
        _emptyView = [[ICEmptyStyleView alloc] initWithTitle:@"暂无搜索结果" placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_message_empty"] imageURL:@""];
        _emptyView.hidden = YES;
        _emptyView.frame = CGRectMake(0, 64, App_Frame_Width, APP_Frame_Height);
        [self.view addSubview:_emptyView];
    }
    return _emptyView;
}

@end
